﻿Imports System.Math
Public Class Disponibilite
    Public oDateToCheck As Date
    Dim oLocalContext As DBMainContext
    Dim oLocalEmp As Employe
    Dim oLocalDispo As Disponibilite_Employe
    Dim oBindingSource As New BindingSource
    Dim iNbrLig As Integer
    Dim iIncrement As Integer
    Dim NeedValidate As Boolean
    Dim LockValidating As Boolean
    Dim oStrHoraire As String
    Dim oDispoEmp As Disponibilite_Employe
    Dim ajout As Boolean
    Dim modif As Boolean
    Dim lecture As Boolean



    Function validation() As Boolean
        Dim valide As Boolean = True

        Dim oItems As List(Of Disponibilite_Employe) = (From u In lstDispo.Items Select CType(u, Disponibilite_Employe)).ToList

        '----vérification dateFin plus grande que la date de début---- 
        If dtpDateFin.Value < dtpDateDebut.Value Then
            Outils.ErrorProviderFunctions.AddError(Me, dtpDateFin, My.Resources.MessageErreur.DateFinInferieur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, dtpDateFin)
        End If

        '----vérification que la date de début et dew fin ne soit pas la même---- pas parfaite mais marche
        If modif = False Then
            For Each Element In oItems
                If dtpDateDebut.Value.Date = Element.PeriodeDebut Or dtpDateFin.Value.Date = Element.PeriodeFin Then
                    Outils.ErrorProviderFunctions.AddError(Me, dtpDateFin, My.Resources.MessageErreur.DatePlageIncorrect)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, dtpDateFin)
                End If
            Next


            '----vérification si dateFin et dateDebut sont entre les autre dispo---- pas tester
            For Each Element In oItems
                If dtpDateDebut.Value.Date >= Element.PeriodeDebut And dtpDateDebut.Value.Date <= Element.PeriodeFin Then
                    Outils.ErrorProviderFunctions.AddError(Me, dtpDateDebut, My.Resources.MessageErreur.DateIncorrect)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, dtpDateDebut)
                End If

                If dtpDateFin.Value.Date >= Element.PeriodeDebut And dtpDateFin.Value.Date <= Element.PeriodeFin Then
                    Outils.ErrorProviderFunctions.AddError(Me, dtpDateFin, My.Resources.MessageErreur.DateIncorrect)
                    valide = False
                Else
                    Outils.ErrorProviderFunctions.RemoveError(Me, dtpDateFin)
                End If
            Next
        End If
        '----vérification de la validiter de tout les chanps----
        If valide = True Then
            Return True
        Else
            Return False
        End If

    End Function
    Sub New(ByVal oContext As DBMainContext, ByVal oDispoEmp As Employe)

        InitializeComponent()
        oLocalEmp = oDispoEmp
        oLocalContext = oContext
        Initialize(oContext, 96, 15)


        lstDispo.DataSource = (From u In oContext.Disponibilite_Employe Where u.ID_Employe = oLocalEmp.ID Select u)
        lstDispo.DisplayMember = "StrDeA"

        'oLocalDispo = (From u In oLocalContext.Disponibilite_Employe Where u.ID = CType(lstDispo.SelectedItem, Disponibilite_Employe).ID Select u).FirstOrDefault
        'reste a empecher le bug kan il ny en a aucun associer a lemploye
        'Me.oDispoEmp = oLocalDispo 
        setHoraire()



        'If lstDispo.Text = "" Then
        '    indisponible()
        'Else
        '    disponible()
        'End If
        indisponible()
        lecture = True
        ajout = False
    End Sub
    Sub disponible()
        'btnModifier.Enabled = True
        'btnSupprimer.Enabled = True
        dtpDateDebut.Enabled = True
        dtpDateFin.Enabled = True
        btnAjouterPlage.Enabled = True
        btnSupprimerPlage.Enabled = True
        'lstDispo.Enabled = True
        dgvSchedule.Enabled = True

    End Sub

    Sub indisponible()
        'btnModifier.Enabled = False
        'btnSupprimer.Enabled = False
        dtpDateDebut.Enabled = False
        dtpDateFin.Enabled = False
        btnAjouterPlage.Enabled = False
        btnSupprimerPlage.Enabled = False
        'lstDispo.Enabled = False
        dgvSchedule.Enabled = False
    End Sub

    Sub Initialize(ByVal oContext As DBMainContext, ByVal nbrLig As Integer, ByVal increment As Integer)
        Dim header As String
        Dim iHeure As Integer = 0
        Dim iMinute As Integer = 0
        Dim oComboColumNames As New DataGridViewComboBoxColumn
        Dim oTextBoxColumn As New DataGridViewTextBoxColumn
        oLocalContext = oContext
        iNbrLig = nbrLig
        iIncrement = increment

        dgvSchedule.Columns.Add("Hr", "Hr")
        dgvSchedule.Columns.Add("Lundi", "Lundi")
        dgvSchedule.Columns.Add("Mardi", "Mardi")
        dgvSchedule.Columns.Add("Mercredi", "Mercredi")
        dgvSchedule.Columns.Add("Jeudi", "Jeudi")
        dgvSchedule.Columns.Add("Vendredi", "Vendredi")
        dgvSchedule.Columns.Add("Samedi", "Samedi")
        dgvSchedule.Columns.Add("Dimanche", "Dimanche")
        dgvSchedule.Columns(0).Frozen = True


        'Initialise la grille
        For i = Outils.ParametreApplication.DebutDeLaJournee To nbrLig - 1
            header = GetHeader(i)
            dgvSchedule.Rows.Add(header)
            dgvSchedule.Columns(0).Width = 38
        Next
        For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
            header = GetHeader(i)
            dgvSchedule.Rows.Add(header)
            dgvSchedule.Columns(0).Width = 38
        Next

        Dim oParam As New Param

        pbDisponible.BackColor = Outils.ParametreApplication.CouleurDispo
        pbNdisponible.BackColor = Outils.ParametreApplication.CouleurNonDispo


    End Sub

    Sub setHoraire()
        If dgvSchedule.Columns.Count > 1 Then

            Dim iSelectedID As Integer = oLocalEmp.ID


            oStrHoraire = Nothing
            If oDispoEmp Is Nothing Then
                oDispoEmp = New Disponibilite_Employe
            End If

            oStrHoraire = oDispoEmp.Lundi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(1).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(1).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(1).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(1).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Mardi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(2).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(2).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(2).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(2).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Mercredi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(3).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(3).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(3).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(3).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Jeudi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(4).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(4).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(4).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(4).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Vendredi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(5).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(5).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(5).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(5).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Samedi
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(6).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(6).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(6).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(6).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            oStrHoraire = oDispoEmp.Dimanche
            If oStrHoraire Is Nothing Or oStrHoraire = "" Then
                oStrHoraire = "".PadLeft(96, "0")
            End If
            For i = Outils.ParametreApplication.DebutDeLaJournee To iNbrLig - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(7).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(7).Style.BackColor = pbNdisponible.BackColor
                End If
            Next

            For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(i) = "1" Then 'Disponible
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(7).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(i) = "0" Then
                    Dim ii As Integer = i
                    Dim orow As DataGridViewRow = (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u).FirstOrDefault
                    orow.Cells(7).Style.BackColor = pbNdisponible.BackColor
                End If
            Next




        End If


    End Sub

    Private Sub dgvSchedule_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSchedule.CellContentClick

    End Sub

    Private Function GetHeader(ByVal iVal As Integer) As String
        Return Floor(iVal / (60 / iIncrement)) & ":" & IIf((((iVal Mod (60 / iIncrement)) * iIncrement) = 0), "00", (iVal Mod 60 / iIncrement) * iIncrement)
    End Function

    Private Sub Disponibilite_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnAjouterPlage.BackColor = Outils.ParametreApplication.CouleurDispo
        btnSupprimerPlage.BackColor = Outils.ParametreApplication.CouleurNonDispo
        'dtpDateDebut.MinDate = Today
        'dtpDateFin.MinDate = Today
    End Sub

    Private Sub btnAjouterPlage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouterPlage.Click
        Dim selectedCellCount As Integer = dgvSchedule.GetCellCount(DataGridViewElementStates.Selected)

        If selectedCellCount > 0 Then

            '    If dgvSchedule.AreAllCellsSelected(True) Then
            '        MessageBox.Show("All cells are selected", "Selected Cells")
            '    Else
            For i = 0 To selectedCellCount - 1
                If dgvSchedule.SelectedCells(i).ColumnIndex <> 0 Then
                    CType(dgvSchedule.SelectedCells(i), DataGridViewTextBoxCell).Style.BackColor = pbDisponible.BackColor
                End If
            Next
            'End If

        End If
    End Sub

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click

        If ajout = True Then
            If validation() = True Then
                modif = False
                oDispoEmp.Lundi = ""
                oDispoEmp.Mardi = ""
                oDispoEmp.Mercredi = ""
                oDispoEmp.Jeudi = ""
                oDispoEmp.Vendredi = ""
                oDispoEmp.Samedi = ""
                oDispoEmp.Dimanche = ""

                For i = 0 To iNbrLig - 1
                    Dim ii As Integer = i
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(1).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Lundi += "1"
                    Else
                        oDispoEmp.Lundi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(2).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Mardi += "1"
                    Else
                        oDispoEmp.Mardi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(3).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Mercredi += "1"
                    Else
                        oDispoEmp.Mercredi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(4).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Jeudi += "1"
                    Else
                        oDispoEmp.Jeudi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(5).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Vendredi += "1"
                    Else
                        oDispoEmp.Vendredi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(6).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Samedi += "1"
                    Else
                        oDispoEmp.Samedi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(7).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oDispoEmp.Dimanche += "1"
                    Else
                        oDispoEmp.Dimanche += "0"
                    End If
                Next


                oDispoEmp.PeriodeDebut = dtpDateDebut.Value
                oDispoEmp.PeriodeFin = dtpDateFin.Value
                oDispoEmp.Employe = (From u In oLocalContext.Employe Where u.ID = oLocalEmp.ID Select u).FirstOrDefault


                oLocalContext.Disponibilite_Employe.InsertOnSubmit(oDispoEmp)


                oLocalContext.SubmitChanges()


                indisponible()
                ajout = False
                lecture = True
                btnAjouter.Image = My.Resources.Images.add2
                btnModifier.Enabled = True
                btnSupprimer.Enabled = True


            End If
        ElseIf lecture = True Then
            btnAjouter.Image = My.Resources.Images.disk_blue
            ajout = True
            lecture = False
            disponible()
            btnModifier.Enabled = False
            btnSupprimer.Enabled = False
            oDispoEmp = New Disponibilite_Employe
            setHoraire()
            dtpDateDebut.Value = Today
            dtpDateFin.Value = Today
        End If


    End Sub

    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click
        
    End Sub

    Private Sub btnSupprimerPlage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimerPlage.Click
        Dim selectedCellCount As Integer = dgvSchedule.GetCellCount(DataGridViewElementStates.Selected)

        If selectedCellCount > 0 Then

            'If dgvSchedule.AreAllCellsSelected(True) Then

            '    MessageBox.Show("All cells are selected", "Selected Cells")

            'Else
            For i = 0 To selectedCellCount - 1
                If dgvSchedule.SelectedCells(i).ColumnIndex <> 0 Then
                    CType(dgvSchedule.SelectedCells(i), DataGridViewTextBoxCell).Style.BackColor = pbNdisponible.BackColor
                End If
            Next
        End If
        'End If
    End Sub

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click


        If modif = True Then
            If validation() = True Then
                ajout = False

                Dim oDispoModif As Disponibilite_Employe
                Dim oLocalDispoModif As New Disponibilite_Employe
                oDispoModif = (From u In oLocalContext.Disponibilite_Employe Where u.ID = oDispoEmp.ID Select u).FirstOrDefault


                oLocalDispoModif.Lundi = ""
                oLocalDispoModif.Mardi = ""
                oLocalDispoModif.Mercredi = ""
                oLocalDispoModif.Jeudi = ""
                oLocalDispoModif.Vendredi = ""
                oLocalDispoModif.Samedi = ""
                oLocalDispoModif.Dimanche = ""

                For i = 0 To iNbrLig - 1
                    Dim ii As Integer = i
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(1).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Lundi += "1"
                    Else
                        oLocalDispoModif.Lundi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(2).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Mardi += "1"
                    Else
                        oLocalDispoModif.Mardi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(3).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Mercredi += "1"
                    Else
                        oLocalDispoModif.Mercredi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(4).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Jeudi += "1"
                    Else
                        oLocalDispoModif.Jeudi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(5).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Vendredi += "1"
                    Else
                        oLocalDispoModif.Vendredi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(6).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Samedi += "1"
                    Else
                        oLocalDispoModif.Samedi += "0"
                    End If
                    If (From u As DataGridViewRow In dgvSchedule.Rows Where u.Cells(0).Value = GetHeader(ii) Select u.Cells(7).Style.BackColor).FirstOrDefault = pbDisponible.BackColor Then
                        oLocalDispoModif.Dimanche += "1"
                    Else
                        oLocalDispoModif.Dimanche += "0"
                    End If
                Next
                oDispoModif.Lundi = oLocalDispoModif.Lundi
                oDispoModif.Mardi = oLocalDispoModif.Mardi
                oDispoModif.Mercredi = oLocalDispoModif.Mercredi
                oDispoModif.Jeudi = oLocalDispoModif.Jeudi
                oDispoModif.Vendredi = oLocalDispoModif.Vendredi
                oDispoModif.Samedi = oLocalDispoModif.Samedi
                oDispoModif.Dimanche = oLocalDispoModif.Dimanche


                oDispoEmp.PeriodeDebut = dtpDateDebut.Value
                oDispoEmp.PeriodeFin = dtpDateFin.Value
                oDispoEmp.Employe = (From u In oLocalContext.Employe Where u.ID = oLocalEmp.ID Select u).FirstOrDefault
                oLocalContext.SubmitChanges()
                indisponible()
                modif = False
                lecture = True
                btnModifier.Image = My.Resources.Images.edit
                btnAjouter.Enabled = True
                btnSupprimer.Enabled = True

            End If

        ElseIf lecture = True Then
            btnModifier.Image = My.Resources.Images.disk_blue
            modif = True
            lecture = False
            disponible()
            btnAjouter.Enabled = False
            btnSupprimer.Enabled = False
        End If

    End Sub

    Private Sub lstDispo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstDispo.SelectedIndexChanged
        If Not lstDispo.SelectedItem Is Nothing Then
            oLocalDispo = (From u In oLocalContext.Disponibilite_Employe Where u.ID = CType(lstDispo.SelectedItem, Disponibilite_Employe).ID Select u).FirstOrDefault

            oDispoEmp = oLocalDispo
            If oDispoEmp.PeriodeDebut Is Nothing Then
                dtpDateDebut.Value = Date.Today
            Else
                dtpDateDebut.Value = oDispoEmp.PeriodeDebut
            End If
            If oDispoEmp.PeriodeFin Is Nothing Then
                dtpDateFin.Value = Date.Today
            Else
                dtpDateFin.Value = oDispoEmp.PeriodeFin
            End If
            setHoraire()
        End If
    End Sub
End Class
